package systemcontract

import (
	"math"
	"math/big"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/consensus/congress/vmcaller"
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/state"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/log"
	"github.com/ethereum/go-ethereum/params"
)

var (
	validatorV1Admin        = common.HexToAddress("0xD849fa4D5475276568eC21f6deBf1C8A889Aa81B")
	validatorV1AdminTestnet = common.HexToAddress("0x0dA5ac74D30D5b3c5ca9167A8666Ca98Fd58d9fb")
)

const (
	validatorV1Code = "0x608060405260043610620002125760003560e01c80638fffcbd0116200011b578063c885bc5811620000a3578063f04a5dcd116200006d578063f04a5dcd146200080c578063f3b1cc67146200083d578063f40f0f521462000855578063f851a440146200088c5762000212565b8063c885bc5814620007bb578063c967f90f14620007d3578063d6c0edad1462000802578063ec0cb33614620002875762000212565b8063afeea11511620000e5578063afeea115146200070c578063bb8b65af1462000724578063bbc71680146200073c578063bcecf81b146200078d5762000212565b80638fffcbd0146200067b5780639001eed814620006c45780639cc02c3014620006dc5780639de7025814620006f45762000212565b80635274ac3f116200019f57806371a1bb75116200016957806371a1bb7514620005fc57806371df76781462000614578063741579b1146200062c5780638f28397014620006445762000212565b80635274ac3f146200035957806360544bf114620004a257806365f69f97146200050c5780636846992a14620005435762000212565b80631c0ffaa211620001e15780631c0ffaa2146200029f5780632e4f67e414620002875780633a82fd5e14620002de57806344f9990014620003255762000212565b806303fab4f61462000217578063136ec0b31462000241578063158ef93e146200025b57806315de360e1462000287575b600080fd5b3480156200022457600080fd5b506200022f620008a4565b60408051918252519081900360200190f35b3480156200024e57600080fd5b5062000259620008b1565b005b3480156200026857600080fd5b506200027362000afc565b604080519115158252519081900360200190f35b3480156200029457600080fd5b506200022f62000b05565b348015620002ac57600080fd5b506200025960048036036040811015620002c557600080fd5b506001600160a01b038135169060200135151562000b0c565b348015620002eb57600080fd5b506200030f600480360360208110156200030457600080fd5b503560ff1662000c2c565b6040805160ff9092168252519081900360200190f35b3480156200033257600080fd5b506200033d62000c41565b604080516001600160a01b039092168252519081900360200190f35b3480156200036657600080fd5b5062000259600480360360608110156200037f57600080fd5b8101906020810181356401000000008111156200039b57600080fd5b820183602082011115620003ae57600080fd5b80359060200191846020830284011164010000000083111715620003d157600080fd5b91908080602002602001604051908101604052809392919081815260200183836020028082843760009201919091525092959493602081019350359150506401000000008111156200042257600080fd5b8201836020820111156200043557600080fd5b803590602001918460208302840111640100000000831117156200045857600080fd5b919080806020026020016040519081016040528093929190818152602001838360200280828437600092019190915250929550505090356001600160a01b0316915062000c479050565b348015620004af57600080fd5b50620004ba6200103e565b60408051602080825283518183015283519192839290830191858101910280838360005b83811015620004f8578181015183820152602001620004de565b505050509050019250505060405180910390f35b3480156200051957600080fd5b506200033d600480360360208110156200053257600080fd5b50356001600160a01b0316620010a2565b3480156200055057600080fd5b5062000259600480360360408110156200056957600080fd5b8101906020810181356401000000008111156200058557600080fd5b8201836020820111156200059857600080fd5b80359060200191846020830284011164010000000083111715620005bb57600080fd5b9190808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152509295505091359250620010bd915050565b3480156200060957600080fd5b506200033d6200156e565b3480156200062157600080fd5b506200025962001574565b3480156200063957600080fd5b506200022f62001703565b3480156200065157600080fd5b5062000259600480360360208110156200066a57600080fd5b50356001600160a01b03166200170f565b3480156200068857600080fd5b506200025960048036036080811015620006a157600080fd5b5060ff813581169160208101358216916040820135811691606001351662001807565b348015620006d157600080fd5b506200022f62001a1f565b348015620006e957600080fd5b506200022f62001a2d565b3480156200070157600080fd5b50620004ba62001a33565b3480156200071957600080fd5b50620004ba62001a95565b3480156200073157600080fd5b506200025962001d98565b3480156200074957600080fd5b506200033d600480360360808110156200076257600080fd5b5080356001600160a01b03908116916020810135909116906040810135906060013560ff1662001fde565b3480156200079a57600080fd5b506200033d60048036036020811015620007b357600080fd5b5035620021c8565b348015620007c857600080fd5b5062000259620021f0565b348015620007e057600080fd5b50620007eb62002258565b6040805161ffff9092168252519081900360200190f35b620002596200225d565b3480156200081957600080fd5b506200030f600480360360208110156200083257600080fd5b503560ff1662002775565b3480156200084a57600080fd5b506200022f6200278a565b3480156200086257600080fd5b506200022f600480360360208110156200087b57600080fd5b50356001600160a01b031662002791565b3480156200089957600080fd5b506200033d620027a3565b68056bc75e2d6310000081565b6000339050806001600160a01b031660076000836001600160a01b0316633a5381b56040518163ffffffff1660e01b815260040160206040518083038186803b158015620008fe57600080fd5b505afa15801562000913573d6000803e3d6000fd5b505050506040513d60208110156200092a57600080fd5b50516001600160a01b039081168252602082019290925260400160002054161462000997576040805162461bcd60e51b8152602060048201526018602482015277159bdd19481c1bdbdb081b9bdd081c9959da5cdd195c995960421b604482015290519081900360640190fd5b336001816001600160a01b031663c19d93fb6040518163ffffffff1660e01b815260040160206040518083038186803b158015620009d457600080fd5b505afa158015620009e9573d6000803e3d6000fd5b505050506040513d602081101562000a0057600080fd5b5051600381111562000a0e57fe5b1462000a53576040805162461bcd60e51b815260206004820152600f60248201526e496e636f727265637420737461746560881b604482015290519081900360640190fd5b6000600a6000836001600160a01b031663683c529c6040518163ffffffff1660e01b815260040160206040518083038186803b15801562000a9357600080fd5b505afa15801562000aa8573d6000803e3d6000fd5b505050506040513d602081101562000abf57600080fd5b5051600181111562000acd57fe5b600181111562000ad957fe5b81526020810191909152604001600020905062000af78183620027b7565b505050565b60005460ff1681565b6201518081565b60005461010090046001600160a01b0316331462000b5e576040805162461bcd60e51b815260206004820152600a60248201526927b7363c9030b236b4b760b11b604482015290519081900360640190fd5b6001600160a01b038281166000908152600760205260409020541662000bb65760405162461bcd60e51b8152600401808060200182810382526021815260200180620061ce6021913960400191505060405180910390fd5b6001600160a01b03808316600090815260076020526040808220548151638ec7a23d60e01b815285151560048201529151931692638ec7a23d9260248084019391929182900301818387803b15801562000c0f57600080fd5b505af115801562000c24573d6000803e3d6000fd5b505050505050565b60016020526000908152604090205460ff1681565b61f00681565b60005460ff161562000c96576040805162461bcd60e51b8152602060048201526013602482015272105b1c9958591e481a5b9a5d1a585b1a5e9959606a1b604482015290519081900360640190fd5b6000835111801562000ca9575081518351145b62000cec576040805162461bcd60e51b815260206004820152600e60248201526d496e76616c696420706172616d7360901b604482015290519081900360640190fd5b6001600160a01b03811662000d40576040805162461bcd60e51b8152602060048201526015602482015274496e76616c69642061646d696e206164647265737360581b604482015290519081900360640190fd5b60008054600160ff199091168117610100600160a81b0319166101006001600160a01b038516021782558180600181111562000d7857fe5b815260200190815260200160002060006101000a81548160ff021916908360ff16021790555060156001600060018081111562000db157fe5b600181111562000dbd57fe5b81526020808201929092526040016000908120805460ff9490941660ff1994851617905560029091527fac33ff75c19e70fe83507db0d683fd3465c996598dc972688b7ace676c89077b805483169055600181527fe90b7bceb6e7df5418fb78d8ee546e97c83a08bbccc01a0644d599ccd2a7c2e080549092169091555b83518160ff16101562001038576000848260ff168151811062000e5a57fe5b6020908102919091018101516001600160a01b0380821660009081526007909352604090922054909250161562000ed4576040805162461bcd60e51b815260206004820152601960248201527856616c696461746f727320616c72656164792065786973747360381b604482015290519081900360640190fd5b600081858460ff168151811062000ee757fe5b602002602001015161271060018060405162000f03906200358c565b80866001600160a01b03168152602001856001600160a01b0316815260200184815260200183600181111562000f3557fe5b815260200182600381111562000f4757fe5b815260200195505050505050604051809103906000f08015801562000f70573d6000803e3d6000fd5b5060068054600181019091557ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f0180546001600160a01b038086166001600160a01b031992831681179093556000928352600760205260408084208054928616929093168217909255815163204a7f0760e21b8152915193945092638129fc1c9260048084019391929182900301818387803b1580156200101057600080fd5b505af115801562001025573d6000803e3d6000fd5b50506001909401935062000e3b92505050565b50505050565b606060048054806020026020016040519081016040528092919081815260200182805480156200109857602002820191906000526020600020905b81546001600160a01b0316815260019091019060200180831162001079575b5050505050905090565b6007602052600090815260409020546001600160a01b031681565b334114620010ff576040805162461bcd60e51b815260206004820152600a6024820152694d696e6572206f6e6c7960b01b604482015290519081900360640190fd5b436000908152600b60209081526040808320600180855292529091205460ff161562001165576040805162461bcd60e51b815260206004820152601060248201526f105b1c9958591e481bdc195c985d195960821b604482015290519081900360640190fd5b818043816200117057fe5b0615620011b7576040805162461bcd60e51b815260206004820152601060248201526f426c6f636b2065706f6368206f6e6c7960801b604482015290519081900360640190fd5b60005460ff16620011fe576040805162461bcd60e51b815260206004820152600c60248201526b139bdd081a5b9a5d081e595d60a21b604482015290519081900360640190fd5b436000908152600b60209081526040808320600180855292528220805460ff191690911790555b60035460ff821610156200128a5760006005600060038460ff16815481106200124a57fe5b6000918252602080832091909101546001600160a01b031683528201929092526040019020805460ff191660ff9290921691909117905560010162001225565b508351620012a09060039060208701906200359a565b5060005b60035460ff82161015620013095760016005600060038460ff1681548110620012c957fe5b6000918252602080832091909101546001600160a01b031683528201929092526040019020805460ff191660ff92909216919091179055600101620012a4565b50620013186004600062003604565b6200132262003627565b50604080518082019091526000808252600160208301525b60028160ff16101562000c2457600060026000848460ff16600281106200135d57fe5b602002015160018111156200136e57fe5b60018111156200137a57fe5b815260200190815260200160002060009054906101000a900460ff1690506000600a6000858560ff1660028110620013ae57fe5b60200201516001811115620013bf57fe5b6001811115620013cb57fe5b8152602081019190915260400160002080549091506001600160a01b03165b60008360ff161180156200140657506001600160a01b03811615155b15620015625760056000826001600160a01b0316633a5381b56040518163ffffffff1660e01b815260040160206040518083038186803b1580156200144a57600080fd5b505afa1580156200145f573d6000803e3d6000fd5b505050506040513d60208110156200147657600080fd5b50516001600160a01b0316815260208101919091526040016000205460ff166200153f576004816001600160a01b0316633a5381b56040518163ffffffff1660e01b815260040160206040518083038186803b158015620014d657600080fd5b505afa158015620014eb573d6000803e3d6000fd5b505050506040513d60208110156200150257600080fd5b505181546001810183556000928352602090922090910180546001600160a01b0319166001600160a01b0390921691909117905560001992909201915b6001600160a01b03908116600090815260038301602052604090205416620013ea565b5050506001016200133a565b61f00581565b6000339050806001600160a01b031660076000836001600160a01b0316633a5381b56040518163ffffffff1660e01b815260040160206040518083038186803b158015620015c157600080fd5b505afa158015620015d6573d6000803e3d6000fd5b505050506040513d6020811015620015ed57600080fd5b50516001600160a01b03908116825260208201929092526040016000205416146200165a576040805162461bcd60e51b8152602060048201526018602482015277159bdd19481c1bdbdb081b9bdd081c9959da5cdd195c995960421b604482015290519081900360640190fd5b60003390506000600a6000836001600160a01b031663683c529c6040518163ffffffff1660e01b815260040160206040518083038186803b1580156200169f57600080fd5b505afa158015620016b4573d6000803e3d6000fd5b505050506040513d6020811015620016cb57600080fd5b50516001811115620016d957fe5b6001811115620016e557fe5b81526020810191909152604001600020905062000af7818362002d75565b670de0b6b3a764000081565b806001600160a01b0381166200175e576040805162461bcd60e51b815260206004820152600f60248201526e496e76616c6964206164647265737360881b604482015290519081900360640190fd5b60005461010090046001600160a01b03163314620017b0576040805162461bcd60e51b815260206004820152600a60248201526927b7363c9030b236b4b760b11b604482015290519081900360640190fd5b60008054610100600160a81b0319166101006001600160a01b03858116820292909217808455604051919004909116917f927cc064d7b7fa546fa7706bc01845d27d06f15af3ae90a672cc44735928e96191a25050565b60005461010090046001600160a01b0316331462001859576040805162461bcd60e51b815260206004820152600a60248201526927b7363c9030b236b4b760b11b604482015290519081900360640190fd5b60ff84830116601514620018a5576040805162461bcd60e51b815260206004820152600e60248201526d496e76616c696420636f756e747360901b604482015290519081900360640190fd5b8360ff168360ff1611158015620018c257508160ff168160ff1611155b6200190c576040805162461bcd60e51b8152602060048201526015602482015274496e76616c6964206261636b757020636f756e747360581b604482015290519081900360640190fd5b7fa6eef7e35abe7026729641147f7915573c7e97b47efa546f5f6e3230263bcb49805460ff86811660ff1992831681179093557fcc69885fda6bcc1a4ace058b4a62bf5e179ea78fd58a1ccd71c22cc9b688792f80548683169084168117909155600260209081527fac33ff75c19e70fe83507db0d683fd3465c996598dc972688b7ace676c89077b8054851689851690811790915560016000527fe90b7bceb6e7df5418fb78d8ee546e97c83a08bbccc01a0644d599ccd2a7c2e080549095169387169384179094556040805195865290850193909352838301526060830152517fef8fc40942f0314a9f5ebd7832ff1b78e6c4b5b7062355066b0c0e3e0edc6f29916080908290030190a150505050565b69010f0cf064dd5920000081565b60065490565b6060600380548060200260200160405190810160405280929190818152602001828054801562001098576020028201919060005260206000209081546001600160a01b0316815260019091019060200180831162001079575050505050905090565b6060600062001aa362003627565b50604080518082019091526000808252600160208301525b60028160ff16101562001bb6576000828260ff166002811062001ada57fe5b602002015190506000600a600083600181111562001af457fe5b600181111562001b0057fe5b815260200190815260200160002090506001600083600181111562001b2157fe5b600181111562001b2d57fe5b8152602081019190915260400160002054600182015460ff918216600160a01b909104909116101562001b74576001810154600160a01b900460ff16949094019362001bab565b6001600083600181111562001b8557fe5b600181111562001b9157fe5b815260208101919091526040016000205460ff1694909401935b505060010162001abb565b5060608260ff1667ffffffffffffffff8111801562001bd457600080fd5b5060405190808252806020026020018201604052801562001bff578160200160208202803683370190505b5090506000805b60028160ff16101562001d8e576000848260ff166002811062001c2557fe5b602002015190506000600a600083600181111562001c3f57fe5b600181111562001c4b57fe5b8152602001908152602001600020905060006001600084600181111562001c6e57fe5b600181111562001c7a57fe5b8152602081019190915260400160002054825460ff90911691506001600160a01b03165b60008260ff1611801562001cba57506001600160a01b03811615155b1562001d7d57806001600160a01b0316633a5381b56040518163ffffffff1660e01b815260040160206040518083038186803b15801562001cfa57600080fd5b505afa15801562001d0f573d6000803e3d6000fd5b505050506040513d602081101562001d2657600080fd5b50518751889060ff891690811062001d3a57fe5b6001600160a01b03928316602091820292909201810191909152918116600090815260038501909252604090912054600190960195600019909201911662001c9e565b50506001909201915062001c069050565b5090935050505090565b6000339050806001600160a01b031660076000836001600160a01b0316633a5381b56040518163ffffffff1660e01b815260040160206040518083038186803b15801562001de557600080fd5b505afa15801562001dfa573d6000803e3d6000fd5b505050506040513d602081101562001e1157600080fd5b50516001600160a01b039081168252602082019290925260400160002054161462001e7e576040805162461bcd60e51b8152602060048201526018602482015277159bdd19481c1bdbdb081b9bdd081c9959da5cdd195c995960421b604482015290519081900360640190fd5b336001816001600160a01b031663c19d93fb6040518163ffffffff1660e01b815260040160206040518083038186803b15801562001ebb57600080fd5b505afa15801562001ed0573d6000803e3d6000fd5b505050506040513d602081101562001ee757600080fd5b5051600381111562001ef557fe5b1462001f3a576040805162461bcd60e51b815260206004820152600f60248201526e496e636f727265637420737461746560881b604482015290519081900360640190fd5b6000600a6000836001600160a01b031663683c529c6040518163ffffffff1660e01b815260040160206040518083038186803b15801562001f7a57600080fd5b505afa15801562001f8f573d6000803e3d6000fd5b505050506040513d602081101562001fa657600080fd5b5051600181111562001fb457fe5b600181111562001fc057fe5b81526020810191909152604001600020905062000af7818362002f6a565b6000805461010090046001600160a01b0316331462002031576040805162461bcd60e51b815260206004820152600a60248201526927b7363c9030b236b4b760b11b604482015290519081900360640190fd5b6001600160a01b0385811660009081526007602052604090205416156200209b576040805162461bcd60e51b815260206004820152601960248201527856616c696461746f727320616c72656164792065786973747360381b604482015290519081900360640190fd5b6000858585856000604051620020b1906200358c565b80866001600160a01b03168152602001856001600160a01b03168152602001848152602001836001811115620020e357fe5b8152602001826003811115620020f557fe5b815260200195505050505050604051809103906000f0801580156200211e573d6000803e3d6000fd5b5060068054600181019091557ff652222313e28459528d920b65115c16c04f3efc82aaedc97be59f3f377c0d3f0180546001600160a01b03808a166001600160a01b03199283168117909355600083815260076020908152604091829020805493871693909416831790935580519182525193945091927f1ab57f2e2a6e4069160cc6501d8012d93ed435770b1ed646f82482a2f7234ff49281900390910190a295945050505050565b60068181548110620021d657fe5b6000918252602090912001546001600160a01b0316905081565b33600090815260096020526040902054806200220d575062002256565b336000818152600960205260408082208290558051600162c261b160e01b03198152905163ff3d9e4f9285926004808201939182900301818588803b15801562000c0f57600080fd5b565b601581565b3341146200229f576040805162461bcd60e51b815260206004820152600a6024820152694d696e6572206f6e6c7960b01b604482015290519081900360640190fd5b436000908152600b6020908152604080832083805290915281205460ff161562002303576040805162461bcd60e51b815260206004820152601060248201526f105b1c9958591e481bdc195c985d195960821b604482015290519081900360640190fd5b60005460ff166200234a576040805162461bcd60e51b815260206004820152600c60248201526b139bdd081a5b9a5d081e595d60a21b604482015290519081900360640190fd5b436000908152600b602090815260408083208380529091528120805460ff191660011790556008546200237f9034906200333e565b905060006200239d60646200239684600a620033a2565b9062003400565b90506000620023b4606462002396856028620033a2565b90506000620023cb606462002396866032620033a2565b60045490915015620025d5576000805b60045460ff82161015620024a257620024976007600060048460ff16815481106200240257fe5b60009182526020808320909101546001600160a01b03908116845283820194909452604092830190912054825163f1cea4c760e01b8152925193169263f1cea4c7926004808201939291829003018186803b1580156200246157600080fd5b505afa15801562002476573d6000803e3d6000fd5b505050506040513d60208110156200248d57600080fd5b505183906200333e565b9150600101620023db565b508015620025d35760005b60045460ff82161015620025d15760006007600060048460ff1681548110620024d257fe5b60009182526020808320909101546001600160a01b039081168452838201949094526040928301822054835163f1cea4c760e01b81529351941694509092620025779287926200239692879263f1cea4c7926004808301939192829003018186803b1580156200254157600080fd5b505afa15801562002556573d6000803e3d6000fd5b505050506040513d60208110156200256d57600080fd5b50518a90620033a2565b6001600160a01b0383166000908152600960205260409020549091506200259f90826200333e565b6001600160a01b038316600090815260096020526040902055620025c4888262003444565b97505050600101620024ad565b505b505b600354156200276c576000805b60035460ff821610156200261457620026096007600060038460ff16815481106200240257fe5b9150600101620025e2565b5060005b60035460ff82161015620027695760006007600060038460ff16815481106200263d57fe5b60009182526020808320909101546001600160a01b03908116845290830193909352604090910181205460035492169250906200267c90869062003400565b905083156200271257600062002700856200239689866001600160a01b031663f1cea4c76040518163ffffffff1660e01b815260040160206040518083038186803b158015620026cb57600080fd5b505afa158015620026e0573d6000803e3d6000fd5b505050506040513d6020811015620026f757600080fd5b505190620033a2565b90506200270e82826200333e565b9150505b6001600160a01b0382166000908152600960205260409020546200273790826200333e565b6001600160a01b0383166000908152600960205260409020556200275c888262003444565b9750505060010162002618565b50505b50505060085550565b60026020526000908152604090205460ff1681565b6206270081565b60096020526000908152604090205481565b60005461010090046001600160a01b031681565b6001820154600160a01b900460ff166200281c5781546001600160a01b0382166001600160a01b0319918216811784556001808501805460ff600160a01b91909516909317838104851690920190931690910260ff60a01b1990911617905562002d71565b81546001600160a01b0382811691161415620028385762002d71565b6001600160a01b0380821660009081526002840160205260409020541680620029e1576001808401805460ff600160a01b80830482169094011690920260ff60a01b1990921691909117908190556040805163f1cea4c760e01b815290516001600160a01b039092169163f1cea4c791600480820192602092909190829003018186803b158015620028c957600080fd5b505afa158015620028de573d6000803e3d6000fd5b505050506040513d6020811015620028f557600080fd5b50516040805163f1cea4c760e01b815290516001600160a01b0385169163f1cea4c7916004808301926020929190829003018186803b1580156200293857600080fd5b505afa1580156200294d573d6000803e3d6000fd5b505050506040513d60208110156200296457600080fd5b505111620029cc57506001820180546001600160a01b038381166000818152600287016020908152604080832080549686166001600160a01b031997881617905586549094168252600388019052919091208054831682179055825490911617905562002d71565b5060018201546001600160a01b031662002b78565b806001600160a01b031663f1cea4c76040518163ffffffff1660e01b815260040160206040518083038186803b15801562002a1b57600080fd5b505afa15801562002a30573d6000803e3d6000fd5b505050506040513d602081101562002a4757600080fd5b50516040805163f1cea4c760e01b815290516001600160a01b0385169163f1cea4c7916004808301926020929190829003018186803b15801562002a8a57600080fd5b505afa15801562002a9f573d6000803e3d6000fd5b505050506040513d602081101562002ab657600080fd5b50511162002ac5575062002d71565b6001600160a01b038083166000818152600386016020526040808220548585168352912080546001600160a01b0319169184169190911790556001850154909116141562002b30576001830180546001600160a01b0319166001600160a01b03831617905562002b78565b6001600160a01b03808316600090815260028501602081815260408084205460038901835281852054861685529290915290912080546001600160a01b031916919092161790555b6001600160a01b0381161580159062002c655750806001600160a01b031663f1cea4c76040518163ffffffff1660e01b815260040160206040518083038186803b15801562002bc657600080fd5b505afa15801562002bdb573d6000803e3d6000fd5b505050506040513d602081101562002bf257600080fd5b50516040805163f1cea4c760e01b815290516001600160a01b0385169163f1cea4c7916004808301926020929190829003018186803b15801562002c3557600080fd5b505afa15801562002c4a573d6000803e3d6000fd5b505050506040513d602081101562002c6157600080fd5b5051115b1562002c8e576001600160a01b0390811660009081526002840160205260409020541662002b78565b6001600160a01b03811662002d045782546001600160a01b038381166000818152600387016020908152604080832080549686166001600160a01b031997881617905588549094168252600288019052828120805485168317905581815291909120805483169055845490911617835562000af7565b6001600160a01b0390811660008181526003850160209081526040808320805487871680865283862080549289166001600160a01b031993841617905582549097168552600289019093528184208054841687179055805483168617905593825292902080549092161790555b5050565b81546001600160a01b0382811691161480159062002dad57506001600160a01b03818116600090815260028401602052604090205416155b1562002db95762002d71565b60018201546001600160a01b038281169116141562002e06576001600160a01b0380821660009081526002840160205260409020546001840180546001600160a01b031916919092161790555b81546001600160a01b038281169116141562002e4a576001600160a01b03808216600090815260038401602052604090205483546001600160a01b03191691161782555b6001600160a01b03808216600090815260038401602052604090205416801562002ea6576001600160a01b038083166000908152600285016020526040808220548484168352912080546001600160a01b031916919092161790555b6001600160a01b03808316600090815260028501602052604090205416801562002f02576001600160a01b038084166000908152600386016020526040808220548484168352912080546001600160a01b031916919092161790555b50506001600160a01b03166000908152600282016020908152604080832080546001600160a01b03199081169091556003850190925290912080549091169055600101805460ff60a01b198116600160a01b9182900460ff9081166000190116909102179055565b6001600160a01b03808216600081815260038501602052604090205460018501549083169216148062002fa457506001600160a01b038116155b80620030855750816001600160a01b031663f1cea4c76040518163ffffffff1660e01b815260040160206040518083038186803b15801562002fe557600080fd5b505afa15801562002ffa573d6000803e3d6000fd5b505050506040513d60208110156200301157600080fd5b50516040805163f1cea4c760e01b815290516001600160a01b0384169163f1cea4c7916004808301926020929190829003018186803b1580156200305457600080fd5b505afa15801562003069573d6000803e3d6000fd5b505050506040513d60208110156200308057600080fd5b505111155b1562003092575062002d71565b6001600160a01b038083166000818152600286016020526040808220548585168352912080546001600160a01b03191691841691909117905584549091161415620030f65782546001600160a01b0319166001600160a01b03821617835562003138565b6001600160a01b0382811660009081526002850160209081526040808320548416835260038701909152902080546001600160a01b0319169183169190911790555b6001600160a01b03811615801590620032255750816001600160a01b031663f1cea4c76040518163ffffffff1660e01b815260040160206040518083038186803b1580156200318657600080fd5b505afa1580156200319b573d6000803e3d6000fd5b505050506040513d6020811015620031b257600080fd5b50516040805163f1cea4c760e01b815290516001600160a01b0384169163f1cea4c7916004808301926020929190829003018186803b158015620031f557600080fd5b505afa1580156200320a573d6000803e3d6000fd5b505050506040513d60208110156200322157600080fd5b5051115b156200324e576001600160a01b0390811660009081526003840160205260409020541662003138565b6001600160a01b038116620032c6576001830180546001600160a01b038481166000818152600288016020908152604080832080549686166001600160a01b031997881617905560038a0190915280822080548616905585549093168152919091208054831682179055825490911617905562000af7565b6001600160a01b0390811660008181526002850160208181526040808420805487168552600390980180835281852080546001600160a01b0319908116998916998a17909155848452895489875283872080549190991690821617909755825283208054861685179055929091529052825416179055565b60008282018381101562003399576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b90505b92915050565b600082620033b3575060006200339c565b82820282848281620033c157fe5b0414620033995760405162461bcd60e51b8152600401808060200182810382526021815260200180620061ad6021913960400191505060405180910390fd5b60006200339983836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f00000000000081525062003488565b60006200339983836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f7700008152506200352f565b60008183620035185760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015620034dc578181015183820152602001620034c2565b50505050905090810190601f1680156200350a5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060008385816200352557fe5b0495945050505050565b60008184841115620035845760405162461bcd60e51b8152602060048201818152835160248401528351909283926044909101919085019080838360008315620034dc578181015183820152602001620034c2565b505050900390565b612b2f806200367e83390190565b828054828255906000526020600020908101928215620035f2579160200282015b82811115620035f257825182546001600160a01b0319166001600160a01b03909116178255602090920191600190910190620035bb565b506200360092915062003645565b5090565b508054600082559060005260206000209081019062003624919062003666565b50565b60405180604001604052806002906020820280368337509192915050565b5b80821115620036005780546001600160a01b031916815560010162003646565b5b808211156200360057600081556001016200366756fe60806040523480156200001157600080fd5b5060405162002b2f38038062002b2f833981810160405260a08110156200003757600080fd5b50805160208201516040830151606084015160809094015160018055929391929091903361f00514620000b1576040805162461bcd60e51b815260206004820152601860248201527f56616c696461746f727320636f6e7472616374206f6e6c790000000000000000604482015290519081900360640190fd5b846001600160a01b03811662000100576040805162461bcd60e51b815260206004820152600f60248201526e496e76616c6964206164647265737360881b604482015290519081900360640190fd5b846001600160a01b0381166200014f576040805162461bcd60e51b815260206004820152600f60248201526e496e76616c6964206164647265737360881b604482015290519081900360640190fd5b838560018260018111156200016057fe5b1415620001b757612710811115620001b1576040805162461bcd60e51b815260206004820152600f60248201526e125b9d985b1a59081c195c98d95b9d608a1b604482015290519081900360640190fd5b62000235565b620001ee600a620001da6003612710620002c660201b620022891790919060201c565b6200032d60201b620022eb1790919060201c565b81111562000235576040805162461bcd60e51b815260206004820152600f60248201526e125b9d985b1a59081c195c98d95b9d608a1b604482015290519081900360640190fd5b6002805462010000600160b01b031916620100006001600160a01b038c81169190910291909117808355600380546001600160a01b031916928c1692909217909155600589905587919060ff1916600183818111156200029157fe5b02179055506002805486919061ff001916610100836003811115620002b257fe5b02179055505050505050505050506200041e565b600082620002d75750600062000327565b82820282848281620002e557fe5b0414620003245760405162461bcd60e51b815260040180806020018281038252602181526020018062002b0e6021913960400191505060405180910390fd5b90505b92915050565b60006200032483836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f0000000000008152506200037760201b60201c565b60008183620004075760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015620003cb578181015183820152602001620003b1565b50505050905090810190601f168015620003f95780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060008385816200041457fe5b0495945050505050565b6126e0806200042e6000396000f3fe6080604052600436106102305760003560e01c80638129fc1c1161012e578063ba26d9ff116100ab578063ec0cb3361161006f578063ec0cb33614610285578063f06d5e7714610614578063f1cea4c71461063e578063f3b1cc6714610653578063ff3d9e4f1461066857610230565b8063ba26d9ff14610591578063c19d93fb146105a6578063c967f90f146105cb578063d0e30db0146105f7578063e9fad8ee146105ff57610230565b806397a8ccd5116100f257806397a8ccd5146104d35780639e83d5b1146104db578063a3ec138d146104f0578063a3fbbaae14610549578063a66066791461057c57610230565b80638129fc1c14610453578063826d3dec146104685780638ec7a23d1461047d5780638f76691a146104a95780639001eed8146104be57610230565b806344f99900116101bc578063683c529c11610180578063683c529c146103b457806370ba1113146103ea57806371a1bb75146103ff57806372a11da414610414578063741579b11461043e57610230565b806344f999001461033a578063481c6a751461034f578063483a00e8146103645780634df9d6ba1461036c57806358fd41ea1461039f57610230565b80632b8aba7a116102035780632b8aba7a146102af5780632e4f67e4146102855780633a5381b5146102c45780633ccfd60b146102f557806341f4ca621461030c57610230565b806303fab4f614610235578063158ef93e1461025c57806315de360e1461028557806324c5b1ca1461029a575b600080fd5b34801561024157600080fd5b5061024a610670565b60408051918252519081900360200190f35b34801561026857600080fd5b5061027161067d565b604080519115158252519081900360200190f35b34801561029157600080fd5b5061024a610686565b3480156102a657600080fd5b5061024a61068d565b3480156102bb57600080fd5b5061024a610693565b3480156102d057600080fd5b506102d9610699565b604080516001600160a01b039092168252519081900360200190f35b34801561030157600080fd5b5061030a6106ae565b005b34801561031857600080fd5b5061032161082c565b6040805192835260208301919091528051918290030190f35b34801561034657600080fd5b506102d9610835565b34801561035b57600080fd5b506102d961083b565b61030a61084a565b34801561037857600080fd5b5061024a6004803603602081101561038f57600080fd5b50356001600160a01b0316610b35565b3480156103ab57600080fd5b5061024a610c5a565b3480156103c057600080fd5b506103c9610cfd565b604051808260018111156103d957fe5b815260200191505060405180910390f35b3480156103f657600080fd5b5061024a610d06565b34801561040b57600080fd5b506102d9610d0c565b34801561042057600080fd5b5061030a6004803603602081101561043757600080fd5b5035610d12565b34801561044a57600080fd5b5061024a611031565b34801561045f57600080fd5b5061030a61103d565b34801561047457600080fd5b5061030a611139565b34801561048957600080fd5b5061030a600480360360208110156104a057600080fd5b503515156112cd565b3480156104b557600080fd5b5061024a6114bb565b3480156104ca57600080fd5b5061024a6114c1565b61030a6114cf565b3480156104e757600080fd5b5061030a61165d565b3480156104fc57600080fd5b506105236004803603602081101561051357600080fd5b50356001600160a01b031661185a565b604080519485526020850193909352838301919091526060830152519081900360800190f35b34801561055557600080fd5b5061030a6004803603602081101561056c57600080fd5b50356001600160a01b0316611881565b34801561058857600080fd5b5061030a611929565b34801561059d57600080fd5b5061030a611af2565b3480156105b257600080fd5b506105bb611c2a565b604051808260038111156103d957fe5b3480156105d757600080fd5b506105e0611c38565b6040805161ffff9092168252519081900360200190f35b61030a611c3d565b34801561060b57600080fd5b5061030a611ec5565b34801561062057600080fd5b5061030a6004803603602081101561063757600080fd5b5035612072565b34801561064a57600080fd5b5061024a6121c2565b34801561065f57600080fd5b5061024a6121c8565b61030a6121cf565b68056bc75e2d6310000081565b60005460ff1681565b6201518081565b600d5481565b600c5481565b6002546201000090046001600160a01b031681565b600260015414156106f4576040805162461bcd60e51b815260206004820152601f6024820152600080516020612610833981519152604482015290519081900360640190fd5b600260015533600090815260096020526040902060030154620151809061071c90439061232d565b11610763576040805162461bcd60e51b8152602060048201526012602482015271125b9d195c9d985b081d1bdbc81cdb585b1b60721b604482015290519081900360640190fd5b336000908152600960205260409020600201546107c2576040805162461bcd60e51b815260206004820152601860248201527756616c75652073686f756c64206e6f74206265207a65726f60401b604482015290519081900360640190fd5b33600081815260096020526040812060028101805490839055600390910191909155906107ef908261236f565b60408051828152905133917f884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364919081900360200190a25060018055565b60065460075482565b61f00681565b6003546001600160a01b031681565b6003546001600160a01b031633146108a0576040805162461bcd60e51b815260206004820152601460248201527313db9b1e481b585b9859d95c88185b1b1bddd95960621b604482015290519081900360640190fd5b6108a8612459565b6108eb576040805162461bcd60e51b815260206004820152600f60248201526e496e636f727265637420737461746560881b604482015290519081900360640190fd5b600d54158061091057506206270061090e600d544361232d90919063ffffffff16565b115b61095c576040805162461bcd60e51b8152602060048201526018602482015277092dce8cae4ecc2d840dcdee840d8dedcce40cadcdeeaced60431b604482015290519081900360640190fd5b600034116109ac576040805162461bcd60e51b815260206004820152601860248201527756616c75652073686f756c64206e6f74206265207a65726f60401b604482015290519081900360640190fd5b6000600d556004546109be90346124b9565b60045560408051348152905133917f278e696bd0cd4a7d1260ced26c40cd01c2b088f441889e4148240ac81069b348919081900360200190a26000600160025460ff166001811115610a0c57fe5b1415610a215750670de0b6b3a7640000610a2e565b5069010f0cf064dd592000005b8060045410610b32576002805461ff0019166101001790819055604080516363e1d45160e01b8152620100009092046001600160a01b031660048301525161f006916363e1d45191602480830192600092919082900301818387803b158015610a9657600080fd5b505af1158015610aaa573d6000803e3d6000fd5b5050505061f0056001600160a01b031663136ec0b36040518163ffffffff1660e01b8152600401600060405180830381600087803b158015610aeb57600080fd5b505af1158015610aff573d6000803e3d6000fd5b5050600254610100900460ff169150506003811115610b1a57fe5b60405160008051602061266a83398151915290600090a25b50565b60408051637a0787a960e11b81523060048201529051600091829161f0059163f40f0f52916024808301926020929190829003018186803b158015610b7957600080fd5b505afa158015610b8d573d6000803e3d6000fd5b505050506040513d6020811015610ba357600080fd5b5051600554909150600090610bc79061271090610bc1908590612289565b906122eb565b600a54600b549192509015610c0e57610c0b81610c05600b54610bc1670de0b6b3a7640000610bff888a61232d90919063ffffffff16565b90612289565b906124b9565b90505b6001600160a01b038516600090815260096020526040902060018101549054610c519190610c4b90670de0b6b3a764000090610bc1908690612289565b9061232d565b95945050505050565b60408051637a0787a960e11b81523060048201529051600091829161f0059163f40f0f52916024808301926020929190829003018186803b158015610c9e57600080fd5b505afa158015610cb2573d6000803e3d6000fd5b505050506040513d6020811015610cc857600080fd5b5051600554909150600090610ce69061271090610bc1908590612289565b600854909150610cf690826124b9565b9250505090565b60025460ff1681565b60055481565b61f00581565b60026001541415610d58576040805162461bcd60e51b815260206004820152601f6024820152600080516020612610833981519152604482015290519081900360640190fd5b600260015580610daa576040805162461bcd60e51b815260206004820152601860248201527756616c75652073686f756c64206e6f74206265207a65726f60401b604482015290519081900360640190fd5b33600090815260096020526040902054811115610e04576040805162461bcd60e51b8152602060048201526013602482015272125b9cdd59999a58da595b9d08185b5bdd5b9d606a1b604482015290519081900360640190fd5b61f0056001600160a01b031663c885bc586040518163ffffffff1660e01b8152600401600060405180830381600087803b158015610e4157600080fd5b505af1158015610e55573d6000803e3d6000fd5b505033600090815260096020526040812060018101549054600a54929450610e9193509091610c4b91670de0b6b3a764000091610bc191612289565b600b54909150610ea1908361232d565b600b5533600090815260096020526040902054610ebe908361232d565b336000908152600960205260409020819055600a54610eeb91670de0b6b3a764000091610bc19190612289565b336000908152600960205260409020600190810191909155600254610100900460ff166003811115610f1957fe5b1415610f755761f0056001600160a01b031663bb8b65af6040518163ffffffff1660e01b8152600401600060405180830381600087803b158015610f5c57600080fd5b505af1158015610f70573d6000803e3d6000fd5b505050505b33600090815260096020526040902060020154610f9290836124b9565b336000818152600960205260409020600281019290925543600390920191909155610fbd908261236f565b60408051838152905133917f41b45db803eded5e27cdf3cbba5707b3575e9b6959de41c3f7b83b51ce600502919081900360200190a260408051828152905133917f7cddc560d4de1ea9d83e4123f01e6072afc503bb47bcc765f0396ba3861a0454919081900360200190a2505060018055565b670de0b6b3a764000081565b3361f0051461108e576040805162461bcd60e51b815260206004820152601860248201527756616c696461746f727320636f6e7472616374206f6e6c7960401b604482015290519081900360640190fd5b60005460ff16156110dc576040805162461bcd60e51b8152602060048201526013602482015272105b1c9958591e481a5b9a5d1a585b1a5e9959606a1b604482015290519081900360640190fd5b6000805460ff191660011781556040805163136ec0b360e01b8152905161f0059263136ec0b3926004808201939182900301818387803b15801561111f57600080fd5b505af1158015611133573d6000803e3d6000fd5b50505050565b3361f00614611186576040805162461bcd60e51b815260206004820152601460248201527350756e69736820636f6e7472616374206f6e6c7960601b604482015290519081900360640190fd5b43600c5560028054610100900460ff1660038111156111a157fe5b146111e2576002805461ff0019166103001790819055610100900460ff1660038111156111ca57fe5b60405160008051602061266a83398151915290600090a25b61f0056001600160a01b03166371df76786040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561121f57600080fd5b505af1158015611233573d6000803e3d6000fd5b50505050600068056bc75e2d6310000060045410156112545760045461125f565b68056bc75e2d631000005b90508015610b3257600454611274908261232d565b60045561128260008261236f565b600254604080518381529051620100009092046001600160a01b0316917febbcaaf6b9aa8b4083ae4b2f842c8de6f75319018e7b5e141a1e87aebadde6c3916020908290030190a250565b3361f0051461131e576040805162461bcd60e51b815260206004820152601860248201527756616c696461746f727320636f6e7472616374206f6e6c7960401b604482015290519081900360640190fd5b80156114245761132c612459565b8061134c57506001600254610100900460ff16600381111561134a57fe5b145b61138f576040805162461bcd60e51b815260206004820152600f60248201526e496e636f727265637420737461746560881b604482015290519081900360640190fd5b6002805461ff0019166102001790819055610100900460ff1660038111156113b357fe5b60405160008051602061266a83398151915290600090a261f0056001600160a01b03166371df76786040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561140757600080fd5b505af115801561141b573d6000803e3d6000fd5b50505050610b32565b60028054610100900460ff16600381111561143b57fe5b1461147f576040805162461bcd60e51b815260206004820152600f60248201526e496e636f727265637420737461746560881b604482015290519081900360640190fd5b6002805461ff00191690819055610100900460ff16600381111561149f57fe5b60405160008051602061266a83398151915290600090a2610b32565b60045481565b69010f0cf064dd5920000081565b60026001541415611515576040805162461bcd60e51b815260206004820152601f6024820152600080516020612610833981519152604482015290519081900360640190fd5b60026001556003546001600160a01b03163314611570576040805162461bcd60e51b815260206004820152601460248201527313db9b1e481b585b9859d95c88185b1b1bddd95960621b604482015290519081900360640190fd5b61f0056001600160a01b031663c885bc586040518163ffffffff1660e01b8152600401600060405180830381600087803b1580156115ad57600080fd5b505af11580156115c1573d6000803e3d6000fd5b5050505060006008541161160d576040805162461bcd60e51b815260206004820152600e60248201526d139bc81b5bdc99481c995dd85c9960921b604482015290519081900360640190fd5b600880546000909155611620338261236f565b60408051828152905133917fe4fc75e2b70d2f179fc77c722f2334ba1507c59932576ec9620b15dfb06d91e2919081900360200190a25060018055565b600260015414156116a3576040805162461bcd60e51b815260206004820152601f6024820152600080516020612610833981519152604482015290519081900360640190fd5b60026001556003546001600160a01b031633146116fe576040805162461bcd60e51b815260206004820152601460248201527313db9b1e481b585b9859d95c88185b1b1bddd95960621b604482015290519081900360640190fd5b611706612459565b611749576040805162461bcd60e51b815260206004820152600f60248201526e496e636f727265637420737461746560881b604482015290519081900360640190fd5b6000600d5411801561177157506206270061176f600d544361232d90919063ffffffff16565b115b6117bd576040805162461bcd60e51b8152602060048201526018602482015277092dce8cae4ecc2d840dcdee840d8dedcce40cadcdeeaced60431b604482015290519081900360640190fd5b600060045411611805576040805162461bcd60e51b815260206004820152600e60248201526d27379036b7b9329036b0b933b4b760911b604482015290519081900360640190fd5b6000600d8190556004805491905561181d338261236f565b60408051828152905133917f5d3b8fa9823b18b176cfe79e002a5b931b8569313802f700eb8550bc6a353246919081900360200190a25060018055565b60096020526000908152604090208054600182015460028301546003909301549192909184565b6002546201000090046001600160a01b031633146118df576040805162461bcd60e51b815260206004820152601660248201527513db9b1e481d985b1a59185d1bdc88185b1b1bddd95960521b604482015290519081900360640190fd5b600380546001600160a01b0319166001600160a01b0383169081179091556040517f5cd5185727f6057b7a274979ce4d902e15bf0ef1dc542d1fe5926cba874f63b690600090a250565b6003546001600160a01b0316331461197f576040805162461bcd60e51b815260206004820152601460248201527313db9b1e481b585b9859d95c88185b1b1bddd95960621b604482015290519081900360640190fd5b60025460065460ff90911690600182600181111561199957fe5b14156119ed576127108111156119e8576040805162461bcd60e51b815260206004820152600f60248201526e125b9d985b1a59081c195c98d95b9d608a1b604482015290519081900360640190fd5b611a45565b6119ff600a610bc16127106003612289565b811115611a45576040805162461bcd60e51b815260206004820152600f60248201526e125b9d985b1a59081c195c98d95b9d608a1b604482015290519081900360640190fd5b60075415801590611a6657506007546201518090611a6490439061232d565b115b611ab2576040805162461bcd60e51b8152602060048201526018602482015277092dce8cae4ecc2d840dcdee840d8dedcce40cadcdeeaced60431b604482015290519081900360640190fd5b600680546005819055600091829055600782905560405190917f450a792501c47863e89114cbdd0497acb22d4abfc51dc315afc323c5ba92d4a991a25050565b3361f00614611b3f576040805162461bcd60e51b815260206004820152601460248201527350756e69736820636f6e7472616374206f6e6c7960601b604482015290519081900360640190fd5b61f0056001600160a01b031663c885bc586040518163ffffffff1660e01b8152600401600060405180830381600087803b158015611b7c57600080fd5b505af1158015611b90573d6000803e3d6000fd5b50505050600068056bc75e2d6310000060085410611bb75768056bc75e2d63100000611bbb565b6008545b600854909150611bcb908261232d565b6008558015610b3257611bdf60008261236f565b600254604080518381529051620100009092046001600160a01b0316917f0a3c8b346f3f7fe5668c9f575473491c4274339e10c9548d7995f22211f988f0916020908290030190a250565b600254610100900460ff1681565b601581565b60026001541415611c83576040805162461bcd60e51b815260206004820152601f6024820152600080516020612610833981519152604482015290519081900360640190fd5b600260018190555061f0056001600160a01b031663c885bc586040518163ffffffff1660e01b8152600401600060405180830381600087803b158015611cc857600080fd5b505af1158015611cdc573d6000803e3d6000fd5b505033600090815260096020526040812060018101549054600a54929450611d1893509091610c4b91670de0b6b3a764000091610bc191612289565b90503415611e395733600090815260096020526040902054611d3a90346124b9565b336000908152600960205260409020819055600a54611d6791670de0b6b3a764000091610bc19190612289565b33600090815260096020526040902060010155600b54611d8790346124b9565b600b5560408051348152905133917fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c919081900360200190a26001600254610100900460ff166003811115611dd857fe5b1415611e345761f0056001600160a01b031663136ec0b36040518163ffffffff1660e01b8152600401600060405180830381600087803b158015611e1b57600080fd5b505af1158015611e2f573d6000803e3d6000fd5b505050505b611e77565b600a5433600090815260096020526040902054611e6391670de0b6b3a764000091610bc191612289565b336000908152600960205260409020600101555b8015611ebe57611e87338261236f565b60408051828152905133917f7cddc560d4de1ea9d83e4123f01e6072afc503bb47bcc765f0396ba3861a0454919081900360200190a25b5060018055565b6003546001600160a01b03163314611f1b576040805162461bcd60e51b815260206004820152601460248201527313db9b1e481b585b9859d95c88185b1b1bddd95960621b604482015290519081900360640190fd5b6001600254610100900460ff166003811115611f3357fe5b1480611f425750611f42612459565b611f85576040805162461bcd60e51b815260206004820152600f60248201526e496e636f727265637420737461746560881b604482015290519081900360640190fd5b43600d556000600254610100900460ff166003811115611fa157fe5b14612033576002805461ff00191690819055610100900460ff166003811115611fc657fe5b60405160008051602061266a83398151915290600090a261f0056001600160a01b03166371df76786040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561201a57600080fd5b505af115801561202e573d6000803e3d6000fd5b505050505b600254604051620100009091046001600160a01b0316907f7c79e6e24ed041d1072d54523b53956f01b91b835f0490856370594d9d14470e90600090a2565b6003546001600160a01b031633146120c8576040805162461bcd60e51b815260206004820152601460248201527313db9b1e481b585b9859d95c88185b1b1bddd95960621b604482015290519081900360640190fd5b60025460ff168160018260018111156120dd57fe5b14156121315761271081111561212c576040805162461bcd60e51b815260206004820152600f60248201526e125b9d985b1a59081c195c98d95b9d608a1b604482015290519081900360640190fd5b612189565b612143600a610bc16127106003612289565b811115612189576040805162461bcd60e51b815260206004820152600f60248201526e125b9d985b1a59081c195c98d95b9d608a1b604482015290519081900360640190fd5b60068390554360075560405183907f2dcbffddb492dea86de0b18dac6d71f51a7b7a5ec946512e0c993a050f3b48ea90600090a2505050565b600b5481565b6206270081565b3361f00514612220576040805162461bcd60e51b815260206004820152601860248201527756616c696461746f727320636f6e7472616374206f6e6c7960401b604482015290519081900360640190fd5b600061223d612710610bc16005543461228990919063ffffffff16565b60085490915061224d90826124b9565b600855600b5415610b3257612283600a54610c05600b54610bc1670de0b6b3a7640000610bff873461232d90919063ffffffff16565b600a5550565b600082612298575060006122e5565b828202828482816122a557fe5b04146122e25760405162461bcd60e51b815260040180806020018281038252602181526020018061268a6021913960400191505060405180910390fd5b90505b92915050565b60006122e283836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f000000000000815250612513565b60006122e283836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f7700008152506125b5565b804710156123c4576040805162461bcd60e51b815260206004820152601d60248201527f416464726573733a20696e73756666696369656e742062616c616e6365000000604482015290519081900360640190fd5b6040516000906001600160a01b0384169083908381818185875af1925050503d806000811461240f576040519150601f19603f3d011682016040523d82523d6000602084013e612414565b606091505b50509050806124545760405162461bcd60e51b815260040180806020018281038252603a815260200180612630603a913960400191505060405180910390fd5b505050565b600080600254610100900460ff16600381111561247257fe5b14806124b457506003600254610100900460ff16600381111561249157fe5b1480156124b45750620151806124b2600c544361232d90919063ffffffff16565b115b905090565b6000828201838110156122e2576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b6000818361259f5760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561256457818101518382015260200161254c565b50505050905090810190601f1680156125915780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060008385816125ab57fe5b0495945050505050565b600081848411156126075760405162461bcd60e51b815260206004820181815283516024840152835190928392604490910191908501908083836000831561256457818101518382015260200161254c565b50505090039056fe5265656e7472616e637947756172643a207265656e7472616e742063616c6c00416464726573733a20756e61626c6520746f2073656e642076616c75652c20726563697069656e74206d61792068617665207265766572746564402ee26d4c255fcb07b0b7b5b93b77377832260977c25be44f3c8feffd2df70e536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f77a26469706673582212208768b36ac385661ffe32f09083eb5e098ed5cb2bbc8366a167befb3e14cc11b764736f6c634300060c0033536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f77536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f77436f72726573706f6e64696e6720766f746520706f6f6c206e6f7420666f756e64a26469706673582212205704491a1a7d78e2f10d1c3eba558c14623a974ac13b6bce6f8a9e56a697c01f64736f6c634300060c0033"
)

type hardForkValidatorsV1 struct {
}

func (s *hardForkValidatorsV1) GetName() string {
	return ValidatorsV1ContractName
}

func (s *hardForkValidatorsV1) Update(config *params.ChainConfig, height *big.Int, state *state.StateDB) (err error) {
	contractCode := common.FromHex(validatorV1Code)

	//write code to sys contract
	state.SetCode(ValidatorsV1ContractAddr, contractCode)
	log.Debug("Write code to system contract account", "addr", ValidatorsV1ContractAddr.String(), "code", validatorV1Code)

	return
}

func (s *hardForkValidatorsV1) getAdminByChainId(chainId *big.Int) common.Address {
	if chainId.Cmp(params.MainnetChainConfig.ChainID) == 0 {
		return validatorV1Admin
	}

	return validatorV1AdminTestnet
}

func (s *hardForkValidatorsV1) Execute(state *state.StateDB, header *types.Header, chainContext core.ChainContext, config *params.ChainConfig) (err error) {

	//First, get top validators from the old contract
	v0 := NewValidatorV0()
	topVals, err := v0.GetTopValidators(state, header, chainContext, config)
	if err != nil {
		log.Error("getTopValidators from V0 failed", "err", err)
		return err
	}
	managers := make([]common.Address, 0, len(topVals))
	for _, val := range topVals {
		feeAddr, err := v0.GetValidatorFeeAddr(val, state, header, chainContext, config)
		if err != nil {
			return err
		}
		managers = append(managers, feeAddr)
	}

	// initialize v1 contract
	method := "initialize"
	data, err := GetInteractiveABI()[ValidatorsV1ContractName].Pack(method, topVals, managers, s.getAdminByChainId(config.ChainID))
	if err != nil {
		log.Error("Can't pack data for initialize", "error", err)
		return err
	}

	msg := vmcaller.NewLegacyMessage(header.Coinbase, &ValidatorsV1ContractAddr, 0, new(big.Int), math.MaxUint64, new(big.Int), data, false)
	_, err = vmcaller.ExecuteMsg(msg, state, header, chainContext, config)

	return
}
